package com.jhs.boot.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jhs.boot.common.annotation.Log;
import com.jhs.boot.common.enums.LogModuleEnum;
import com.jhs.boot.common.model.Option;
import com.jhs.boot.common.result.PageResult;
import com.jhs.boot.common.result.Result;
import com.jhs.boot.system.model.form.SysNodesForm;
import com.jhs.boot.system.model.query.SysNodesQuery;
import com.jhs.boot.system.model.vo.GraphNodeVO;
import com.jhs.boot.system.model.vo.SysNodesVO;
import com.jhs.boot.system.service.SysNodesService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 节点前端控制层
 *
 * @author zxy
 * @since 2025-08-12 10:33
 */
@Tag(name = "节点接口")
@RestController
@RequestMapping("/api/v1/sys-nodes")
@RequiredArgsConstructor
public class SysNodesController  {

    private final SysNodesService sysNodesService;
    @Operation(summary = "选择下拉列表")
    @GetMapping("/options")
    public Result<List<Option<Long>>> listRouteOptions(
            @Parameter(description = "是否只查询父级菜单")
            @RequestParam(required = false, defaultValue = "false") boolean onlyParent
    ) {
        List<Option<Long>> routeOptions = sysNodesService.listNodeOptions(onlyParent);
        return Result.success(routeOptions);
    }
    @Operation(summary = "节点分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('ap:nodes:query')")
    public PageResult<SysNodesVO> getSysNodesPage(SysNodesQuery queryParams ) {
        IPage<SysNodesVO> result = sysNodesService.getSysNodesPage(queryParams);
        return PageResult.success(result);
    }
    @Operation(summary = "新增节点")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('ap:nodes:add')")
    @Log( value = "新增节点",module = LogModuleEnum.NODE)
    public Result<Void> saveSysNodes(@RequestBody @Valid SysNodesForm formData ) {
        boolean result = sysNodesService.saveSysNodes(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取节点表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('ap:nodes:edit')")
    public Result<SysNodesForm> getSysNodesForm(
            @Parameter(description = "节点ID") @PathVariable Long id
    ) {
        SysNodesForm formData = sysNodesService.getSysNodesFormData(id);
        return Result.success(formData);
    }
    @Operation(summary = "修改节点")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('ap:nodes:edit')")
    @Log( value = "修改节点",module = LogModuleEnum.NODE)
    public Result<Void> updateSysNodes(
            @Parameter(description = "节点ID") @PathVariable Long id,
            @RequestBody @Validated SysNodesForm formData
    ) {
        boolean result = sysNodesService.updateSysNodes(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除节点")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('ap:nodes:delete')")
    @Log( value = "删除节点",module = LogModuleEnum.NODE)
    public Result<Void> deleteSysNodess(
            @Parameter(description = "节点ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = sysNodesService.deleteSysNodess(ids);
        return Result.judge(result);
    }
    @Operation(summary = "获取用户下拉节点MPP MAP树选项")
    @GetMapping("/tree")
    public Result<List<Option<String>>> listUserOptions() {
        List<Option<String>> list = sysNodesService.listNodesOptions();
        return Result.success(list);
    }
    @Operation(summary = "获取节点网络拓扑图")
    @GetMapping("/graph")
    public Result<GraphNodeVO> graphOptions() {
        return Result.success(sysNodesService.graphOptions());
    }
    @Operation(summary = "获取所有节点")
    @GetMapping("/getAllNodes")
    public Result<List<GraphNodeVO>> getAllNodes() {
        List<GraphNodeVO> list = sysNodesService.getAllNodes();
        return Result.success(list);
    }

}
